#include "login.h"

void lcd_draw_circle(int row, int col, int halflen, int color) //参数为圆心坐标和半径
{
	int x, y;
	for(x=row-halflen; x<=row+halflen; x++)
	{	
		y= col;
		while((y-col)*(y-col) <= (halflen*halflen - (x-row)*(x-row)))
		{
			lcd_draw_point(x,y,color);
			y++;
		}
		
		y= col;
		while((col-y)*(col-y) <= (halflen*halflen - (x-row)*(x-row)))
		{
			lcd_draw_point(x,y,color);
			y--;
		}	
	}
}

char keyboard_locate(int x, int y)			//	键盘标记
{
	char a;
	if(x>=270&&x<355 &&y>=160&&y<240)
		a = '1';
	else if(x>=355&&x<440 &&y>=160&&y<240)
		a = '2';
	else if(x>=440&&x<520 &&y>=160&&y<240)
		a = '3';
	else if(x>=270&&x<355 &&y>=240&&y<320)
		a = '4';
	else if(x>=355&&x<440 &&y>=240&&y<320)
		a = '5';
	else if(x>=440&&x<520 &&y>=240&&y<320)
		a = '6';
	else if(x>=270&&x<355 &&y>=320&&y<390)
		a = '7';
	else if(x>=355&&x<440 &&y>=320&&y<390)
		a = '8';
	else if(x>=440&&x<520 &&y>=320&&y<390)
		a = '9';
	else if(x>=270&&x<355 &&y>=390&&y<470)	//退出
		a = 'e';
	else if(x>=355&&x<440 &&y>=390&&y<470)
		a = '0';
	else if(x>=440&&x<520 &&y>=390&&y<470)	//删除
		a = 'd';
	else
		a = 'f';
		
	return a;
}


void show_login(void)
{
	struct coordinate coor;
	bool released_new = false;
	bool released_old = false;
	char input_passwd[7],passwd[7],t;							//定义数组记录输入的密码和读取passwd中的密码
	int time=0;//记录按下次数
	int fd_passwd;
	
	
	bzero(input_passwd,7);
	bzero(passwd,7);
	fd_passwd = open(PASSWD_PATH,O_RDONLY);
	if(fd_passwd == -1)
	{
		perror("open passwd failed\n");
		return;
	}
	read(fd_passwd,passwd,6);
	printf("len(passwd) = %d\n",strlen(passwd));
	printf("passwd = %s\n",passwd);
	
	
	while(1)
	{
		lcd_draw_bmp(0,0,SHOW_PASSWD);
		lcd_draw_bmp(0,160,SHOW_KEY);
		bzero(input_passwd,7);
		t=0;
		time=0;
		while(1)
		{
			released_new = true;
			ts_trace(ts_fd, &coor, &released_new);
			printf("released_new = %d\n", released_new);
			printf("x = %d\n", coor.x);
			printf("y = %d\n", coor.y);
			if(released_new)
			{
				lcd_draw_bmp(0,160,SHOW_KEY);
			}
			
			printf("=============\n");
			if(!released_new && released_new != released_old)
			{
				t = keyboard_locate(coor.x,coor.y);
				printf("t = %c\n", t);
				switch(t)											//判断触摸哪个区域，在对应																
				{													//区域刷一张图片以显示按下
					case '1':lcd_draw_circle(311, 206, 26, PASSWD_COLOR);
							break;
					case '2':lcd_draw_circle(395,206, 26, PASSWD_COLOR);
							break;
					case '3':lcd_draw_circle(482,206, 26, PASSWD_COLOR);
							break;
					case '4':lcd_draw_circle(311, 280, 26, PASSWD_COLOR);
							break;
					case '5':lcd_draw_circle(395, 280, 26, PASSWD_COLOR);
							break;
					case '6':lcd_draw_circle(482, 280, 26, PASSWD_COLOR);
							break;
					case '7':lcd_draw_circle(311, 354, 26, PASSWD_COLOR);
							break;
					case '8':lcd_draw_circle(395, 354, 26, PASSWD_COLOR);
							break;
					case '9':lcd_draw_circle(482, 354, 26, PASSWD_COLOR);
							break;
					case 'e':lcd_draw_circle(311, 428, 26, PASSWD_COLOR);
							break;
					case '0':lcd_draw_circle(395, 428, 26, PASSWD_COLOR);
							break;
					case 'd':lcd_draw_circle(482, 428, 26, PASSWD_COLOR);
							break;
					default:break;
				}

				if(t>='0' && t<='9')					//记录输入的密码并在密码显示区刷一张*
				{
					input_passwd[time] = t;
					time++;
					if(time>=6)
						time = 6;
					lcd_draw_bmp(28*(time-1)+310,117,PASSWD);
				}
				else if (t == 'd')
				{
					input_passwd[time] = '\0';
					time--;			
					if( time <= 0)
						time = 0;
					lcd_draw_bmp(28*(time)+310,117,PASSWD_CLEAR);			//按下删除键时，刷一张白色图片
				}
				
				if(time == 6)
				{
				
					printf("input_passwd = %s\n",input_passwd);
					printf("len(input_passwd) = %d\n",strlen(input_passwd));
					if(time == 6)
					{
						
						if (strncmp(passwd,input_passwd,6)==0)									//登录成功
						{
							printf("denglu ok!\n");
							lcd_draw_bmp(0,160,SHOW_KEY);
							return;												//退出登录界面
						}
						else
						{
							lcd_draw_bmp(0,0,SHOW_PASSWD);
							lcd_draw_bmp(0,160,SHOW_KEY);
							usleep(20*1000);
							lcd_draw_bmp(170,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(185,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(200,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(215,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(230,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(215,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(200,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(185,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(170,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(185,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							lcd_draw_bmp(200,99,SHOW_PASSWD_WRONG);
							usleep(20*1000);
							break;
						}
					}

				}
			}	
		released_old = released_new;
		}
		continue;
	}
}


